﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace sy2_11
{
    public partial class Form1 : Form
    {
        int[] randomNums;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                int count = int.Parse(txtNumCount.Text);
                string[] rangeStr = txtRange.Text.Split('~');
                int min = int.Parse(rangeStr[0].Trim());
                int max = int.Parse(rangeStr[1].Trim());
                Random random = new Random();
                randomNums = new int[count];
                string result = "";
                for (int i = 0; i < count; i++)
                {
                    randomNums[i] = random.Next(min, max + 1);
                    result += randomNums[i] + Environment.NewLine;
                }
                txtRandomNums.Text = result;
            }
            catch (Exception ex)
            {
                MessageBox.Show("输入错误：" + ex.Message);
            }
        }

        private void btnFindPrimes_Click(object sender, EventArgs e)
        {
            if (randomNums == null)
            {
                MessageBox.Show("请先点击产生按钮生成随机数");
                return;
            }
            string primesResult = "";
            foreach (int num in randomNums)
            {
                if (IsPrime(num))
                {
                    primesResult += num + Environment.NewLine;
                }
            }
            txtPrimes.Text = primesResult;
        }

        private bool IsPrime(int num)
        {
            if (num <= 1)
            {
                return false;
            }
            for (int i = 2; i * i <= num; i++)
            {
                if (num % i == 0)
                {
                    return false;
                }
            }
            return true;
        }
    }
}
